package com.johnny.xia.sothebys.checkdata;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.mountcloud.graphql.GraphqlClient;
import org.mountcloud.graphql.request.query.DefaultGraphqlQuery;
import org.mountcloud.graphql.request.query.GraphqlQuery;
import org.mountcloud.graphql.request.result.ResultAttributtes;
import org.mountcloud.graphql.response.GraphqlResponse;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class CheckData {

    static String url="https://proxyapi-prod.sothebys.cn/graphql";

    public static void main(String[] args) throws IOException {
        int i=0;
        for (String auctionId : DataAuctions.datas){
            i+= ckeckLots(auctionId);
            return;
        }

        System.out.printf("i:"+i);
    }

    public static int ckeckLots(String auctionId) throws IOException {
        GraphqlClient client = GraphqlClient.buildGraphqlClient(url);
        Map httpHeaders = new HashMap<>();
        httpHeaders.put("Content-Type","application/json");
        client.setHttpHeaders(httpHeaders);
        GraphqlQuery query = new DefaultGraphqlQuery("auction");
        query.addParameter("id",auctionId);
        ResultAttributtes resAttr = new ResultAttributtes("lotCards(filter:ALL)");
        ResultAttributtes lotId = new ResultAttributtes("lotId");
//        ResultAttributtes objects = new ResultAttributtes("objects");
//        ResultAttributtes metadata = new ResultAttributtes("metadata");
//        metadata.addResultAttributes("key");

//        ResultAttributtes _valueAttr = new ResultAttributtes("value");
//        _valueAttr.addResultAttributes("__typename");

//        metadata.addResultAttributes(_valueAttr);
//        objects.addResultAttributes(metadata);
//        resAttr.addResultAttributes(lotId,objects);
        query.addResultAttributes(resAttr);

        GraphqlResponse response = client.doQuery(query);
        Map map = response.getData();
        String jsonData = JSON.toJSONString(map);

        JSONObject json = JSON.parseObject(jsonData);
        JSONArray lotCards = json.getJSONObject("data").getJSONObject("auction").getJSONArray("lotCards");
        return lotCards.size();
//        for(int i=0;i<lotCards.size();i++){
//            JSONObject jo = lotCards.getJSONObject(i);
//            JSONArray objectsTmp = jo.getJSONArray("objects");
//            JSONObject objTmp = objectsTmp.getJSONObject(0);
//            JSONArray metaDataArray = objTmp.getJSONArray("metadata");
//            for(int j=0;j<metaDataArray.size();j++){
//                JSONObject objT = metaDataArray.getJSONObject(j);
//                String keyTmp = objT.getString("key");
//
//                String _valueType = objT.getJSONObject("value").getString("__typename");
//
//                String _vt = vtMap.get(_valueType);
//
//                String mapV = metadataKey.get(keyTmp);
//                if(mapV != null && !mapV.equals(_vt)){
//                    throw new RuntimeException("key一样，类型不一样"+ keyTmp+"--"+_vt +"-->"+mapV);
//                }
//                metadataKey.put(keyTmp,_vt);
//            }
//        }
    }

}
